clear
set more off
cd "/Users/shahbanoijaz/Dropbox/Political Violence/data"


use master_dy_panel.dta
 
capture drop milhq_self afg_neighbor

g milhq_self=.

replace milhq_self=1 if district=="Bahawalpur"
replace milhq_self=1 if district=="Gilgit"
replace milhq_self=1 if district=="Gujranwala"
replace milhq_self=1 if district=="Hyderabad"
replace milhq_self=1 if district=="Jhelum"
replace milhq_self=1 if district=="Gujrat"
replace milhq_self=1 if district=="Khuzdar"
replace milhq_self=1 if district=="Kohat"
replace milhq_self=1 if district=="Lahore"
replace milhq_self=1 if district=="Malir"
replace milhq_self=1 if district=="Rawalpindi"
replace milhq_self=1 if district=="Multan"
replace milhq_self=1 if district=="Nowshera"
replace milhq_self=1 if district=="Okara"
replace milhq_self=1 if district=="Sukkur"
replace milhq_self=1 if district=="Peshawar"
replace milhq_self=1 if district=="Quetta"
replace milhq_self=1 if district=="Sargodha"
replace milhq_self=1 if district=="Sialkot"
replace milhq_self=1 if district=="Kech"

replace milhq_self=0 if milhq_self==.

g afg_neighbor=.
replace afg_neighbor=1 if district== "Chagi"
replace afg_neighbor=1 if district== "Noshki"	
replace afg_neighbor=1 if district== "Quetta"
replace afg_neighbor=1 if district== "Killa Abdullah"
replace afg_neighbor=1 if district== "Pishin"
replace afg_neighbor=1 if district== "Killa Saifullah"
replace afg_neighbor=1 if district== "Zhob"
replace afg_neighbor=1 if district== "Chitral"
replace afg_neighbor=1 if district== "Lower Dir"
replace afg_neighbor=1 if district== "Upper Dir"
replace afg_neighbor=1 if district== "Bajour"
replace afg_neighbor=1 if district== "Mohmand"
replace afg_neighbor=1 if district== "Khyber"
replace afg_neighbor=1 if district== "Kurram"
replace afg_neighbor=1 if district== "North Waziristan"
replace afg_neighbor=1 if district== "South Waziristan" 
replace afg_neighbor=0 if afg_neighbor==. 

g isl_neighbor=.
replace isl_neighbor=1 if district=="Abbottabad"
replace isl_neighbor=1 if district=="Haripur"
replace isl_neighbor=1 if district=="Rawalpindi"
replace isl_neighbor=1 if district=="Attock"
replace isl_neighbor=0 if isl_neighbor==.

g isl_neighbor2=.
replace isl_neighbor2=1 if district=="Abbottabad"
replace isl_neighbor2=1 if district=="Haripur"
replace isl_neighbor2=1 if district=="Rawalpindi"
replace isl_neighbor2=1 if district=="Attock"
replace isl_neighbor2=1 if district=="Swabi"
replace isl_neighbor2=1 if district=="Buner"
replace isl_neighbor2=1 if district=="Mansehra"
replace isl_neighbor2=1 if district=="Mianwali"
replace isl_neighbor2=1 if district=="Chakwal"
replace isl_neighbor2=1 if district=="Jhelum"
replace isl_neighbor2=1 if district=="Muzaffarabad"
replace isl_neighbor2=1 if district=="Poonch"
replace isl_neighbor2=0 if isl_neighbor2==.


capture drop laid2
g laid2 = log(milaid + 1)
capture drop idist
g idist= km_to_nid


capture drop _merge

merge m:1 id05 using milhqnbr.dta 

replace milhq=0 if milhq==. 
g milhq_nbr=.
replace milhq_nbr=1 if milhq_self==0 & milhq==1 
replace milhq_nbr=0 if milhq_nbr==. & milhq_self==0

g milhq_nbrbase=.
replace milhq_nbrbase=1 if milhq_self==0 & milhq==1
replace milhq_nbrbase=0 if milhq_nbrbase==.


capture drop _merge
merge m:1 year using chinaarms.dta
bysort year: gen lchina=log(china_transfer + 1)

set more off


bysort year: egen meankilled_base=mean(numberkilled) if km_to_nid<173
bysort year: egen meankilled_nobase=mean(numberkilled) if km_to_nid>173

tsline laid meankilled_base meankilled_nobase

bysort year: egen meanattacks_base=mean(eventsucc) if km_to_nid<173
bysort year: egen meanattacks_nobase=mean(numberkilled) if km_to_nid>173

bysort year: gen meanattacks_diff=.
replace 	meanattacks_diff=	0.176697	if meanattacks_diff==.	& year==1988
replace 	meanattacks_diff=	-4.111686	if meanattacks_diff==.	& year==1989
replace 	meanattacks_diff=	-10.963139	if meanattacks_diff==.	& year==1990
replace 	meanattacks_diff=	-1.387255	if meanattacks_diff==.	& year==1991
replace 	meanattacks_diff=	1.649123	if meanattacks_diff==.	& year==1992
replace 	meanattacks_diff=	-2.644928	if meanattacks_diff==.	& year==1993
replace 	meanattacks_diff=	7.22564	if meanattacks_diff==.	& year==	1994
replace 	meanattacks_diff=	17.632271	if meanattacks_diff==.	& year==1995
replace 	meanattacks_diff=	7.958336	if meanattacks_diff==.	& year==1996
replace 	meanattacks_diff=	1.113871	if meanattacks_diff==.	& year==1997
replace 	meanattacks_diff=	10.761901	if meanattacks_diff==.	& year==1998
replace 	meanattacks_diff=	1.12745	if meanattacks_diff==.	& year==1999
replace 	meanattacks_diff=	7.414486	if meanattacks_diff==.	& year==2000
replace 	meanattacks_diff=	4.23371	if meanattacks_diff==.	& year==2001
replace 	meanattacks_diff=	0.990109	if meanattacks_diff==.	& year==2002
replace 	meanattacks_diff=	0.677861	if meanattacks_diff==.	& year==2003
replace 	meanattacks_diff=	-12.272899	if meanattacks_diff==.	& year==2004
replace 	meanattacks_diff=	-15.377203	if meanattacks_diff==.	& year==2005
replace 	meanattacks_diff=	-36.08654	if meanattacks_diff==.	& year==2006
replace 	meanattacks_diff=	-48.330962	if meanattacks_diff==.	& year==2007
replace 	meanattacks_diff=	-126.253204	if meanattacks_diff==.	& year==2008
replace 	meanattacks_diff=	-143.71264	if meanattacks_diff==.	& year==2009
replace 	meanattacks_diff=	-71.36957	if meanattacks_diff==.	& year==2010

bysort year: gen meanattacksdiff2=.
replace 	meanattacksdiff2=	-0.176697	if meanattacksdiff2==. & year==	1988
replace 	meanattacksdiff2=	4.111686	if meanattacksdiff2==. & year==	1989
replace 	meanattacksdiff2=	10.963139	if meanattacksdiff2==. & year==	1990
replace 	meanattacksdiff2=	1.387255	if meanattacksdiff2==. & year==	1991
replace 	meanattacksdiff2=	-1.649123	if meanattacksdiff2==. & year==	1992
replace 	meanattacksdiff2=	2.644928	if meanattacksdiff2==. & year==	1993
replace 	meanattacksdiff2=	-7.22564	if meanattacksdiff2==. & year==	1994
replace 	meanattacksdiff2=	-17.632271	if meanattacksdiff2==. & year==	1995
replace 	meanattacksdiff2=	-7.958336	if meanattacksdiff2==. & year==	1996
replace 	meanattacksdiff2=	-1.113871	if meanattacksdiff2==. & year==	1997
replace 	meanattacksdiff2=	-10.761901	if meanattacksdiff2==. & year==	1998
replace 	meanattacksdiff2=	-1.12745	if meanattacksdiff2==. & year==	1999
replace 	meanattacksdiff2=	-7.414486	if meanattacksdiff2==. & year==	2000
replace 	meanattacksdiff2=	-4.23371	if meanattacksdiff2==. & year==	2001
replace 	meanattacksdiff2=	-0.990109	if meanattacksdiff2==. & year==	2002
replace 	meanattacksdiff2=	-0.677861	if meanattacksdiff2==. & year==	2003
replace 	meanattacksdiff2=	12.272899	if meanattacksdiff2==. & year==	2004
replace 	meanattacksdiff2=	15.377203	if meanattacksdiff2==. & year==	2005
replace 	meanattacksdiff2=	36.08654	if meanattacksdiff2==. & year==	2006
replace 	meanattacksdiff2=	48.330962	if meanattacksdiff2==. & year==	2007
replace 	meanattacksdiff2=	126.253204	if meanattacksdiff2==. & year==	2008
replace 	meanattacksdiff2=	143.71264	if meanattacksdiff2==. & year==	2009
replace 	meanattacksdiff2=	71.36957	if meanattacksdiff2==. & year==	2010

//Replicating Figure 4
tsline meanattacksdiff2 || tsline laid2, yaxis(2) 

tsline 


***///RD Results (Replicating Table 3 and 4)///***

//REGRESSION DISCONTINUITY//

g running2002 = year - 2001.5
g running1990 = 1990.5 - year 

g treat1990 =.
replace treat1990 = 1 if running1990>0
replace treat1990 = 0 if running1990<0 

g treat2002=.
replace treat2002=1 if running2002>0
replace treat2002=0 if running2002<0

g treat1990Xrunning1990 = treat1990*running1990
g treat2002Xrunning2002 = treat2002*running2002

tab id05, gen(District)

g laid2Xidist = laid2*idist 
g treat1990Xdist = treat1990*idist
g treat2002Xdist = treat2002*idist 

//RD-1990

**IK bandwidth 

rdob eventocc running1990, c(0)  fuzzy(laid2)

//Optimal bandwidth is 3.84
reg eventocc treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=2, cluster(year) 
eststo bw19902

reg eventocc treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=3, cluster(year) 
eststo bw19903

reg eventocc treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=4, cluster(year) 
eststo bw19904

reg eventocc treat1990 treat1990Xdist running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=2, cluster(year)
eststo bw1990dist1

reg eventocc treat1990 treat1990Xdist running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=3, cluster(year)
eststo bw1990dist2

reg eventocc treat1990 treat1990Xdist running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=4, cluster(year)
eststo bw1990dist3

esttab bw1990dist1 bw1990dist2 bw1990dist3 using 12nov_bw1990dist.tex, replace keep(treat1990 running1990 treat1990Xdist treat1990Xrunning1990) se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)

esttab bw19902 bw19903 bw19904 using 12nov_rd1990.tex, replace keep(treat1990 running1990 treat1990Xrunning1990) se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)


//RD 2002

**IK bandwidth 

rdob eventocc running2002, c(0)  fuzzy(laid2)

//optimal bandwidth is 4.17

**testing IV manually


reg eventocc treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=2, cluster(year) 
eststo bw20022

reg eventocc treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=3, cluster(year) 
eststo bw20023

reg eventocc treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=4, cluster(year) 
eststo bw20024

reg eventocc treat2002 treat2002Xdist running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=2, cluster(year)
eststo bw2002dist1

reg eventocc treat2002 treat2002Xdist running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=3, cluster(year)
eststo bw2002dist2
reg eventocc treat2002 treat2002Xdist running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=4, cluster(year)
eststo bw2002dist3

esttab bw2002dist1 bw2002dist2 bw2002dist3 using 12nov_bw2002dist.tex, replace se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) keep(treat2002 running2002 treat2002Xdist treat2002Xrunning2002)

esttab bw20022 bw20023 bw20024 using 12nov_rd2002.tex, replace se(2) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep(treat2002 running2002 treat2002Xrunning2002)

//RDKILLED

reg numberkilled treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=2, cluster(year) 
eststo rdk1

reg numberkilled treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=3, cluster(year) 
eststo rdk2

reg numberkilled treat1990 running1990 treat1990Xrunning1990 i.id05 if abs(running1990)<=4, cluster(year) 
eststo rdk3

reg numberkilled treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=2, cluster(year) 
eststo rdk4

reg numberkilled treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=3, cluster(year) 
eststo rdk5

reg numberkilled treat2002 running2002 treat2002Xrunning2002 i.id05 if abs(running2002)<=4, cluster(year) 
eststo rdk6

esttab rdk1 rdk2 rdk3 rdk4 rdk5 rdk6 using 22jun_rdk.tex, replace se(2) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep(treat1990 treat2002 treat1990Xrunning1990 treat2002Xrunning2002)


//RD PLACEBO
capture drop treat1999
g treat1999=.
replace treat1999=1 if year>1999.5
replace treat1999=0 if year<1999.5
g running1999=year-1999
g treat1999Xrunning1999=treat1999*running1999

reg eventocc treat1999 running1999 treat1999Xrunning1999 i.id05 if abs(running1999)<=3, cluster(year) 
eststo placebo1


capture drop treat2007
g treat2007=.
replace treat2007=1 if year>2007.5
replace treat2007=0 if year<2007.5
g running2007=year-2007
g treat2007Xrunning2007=treat2007*running2007

reg eventocc treat2007 running2007 treat2007Xrunning2007 i.id05 if abs(running2007)<=3, cluster(year) 
eststo placebo2

esttab placebo1 placebo2 using 12nov_rdplacebo.tex, replace se(2) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep(treat1999 treat2007 treat1999Xrunning1999 treat2007Xrunning2007 running1999 running2007)


//RD POOLED

* Pooled estimate

clear 
use datapooled.dta

g treat_pooled=.
replace treat_pooled=1 if running_pooled>0
replace treat_pooled=0 if running_pooled<0

g treat_pooledXrunning_pooled = treat_pooled*running_pooled
g treat_pooledXdist = treat_pooled*idist 

g laid2Xdist = laid2*idist

**IK bandwidth

rdob eventocc running_pooled, c(0)  fuzzy(laid2)

//optimal bandwidth is 6

//rd reduced form : get a -ve result
reg eventocc running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=2, cluster(year) robust
eststo rdpoolbw2

reg eventocc running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=3, cluster(year) robust
eststo rdpoolbw3

reg eventocc running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=4, cluster(year) robust
eststo rdpoolbw4

reg eventocc running_pooled treat_pooledXdist treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=2, cluster(year) robust
eststo rdpooldist1

reg eventocc running_pooled  treat_pooledXdist treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=3, cluster(year) robust
eststo rdpooldist2

reg eventocc running_pooled treat_pooledXdist treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=4, cluster(year) robust
eststo rdpooldist3

esttab rdpooldist1 rdpooldist2 rdpooldist3 using 12nov_rdpooldist.tex, replace se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) keep(running_pooled treat_pooled treat_pooledXdist treat_pooledXrunning_pooled)

esttab rdpoolbw2 rdpoolbw3 rdpoolbw4 using  12nov_pooledrd.tex, replace se(2) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep(running_pooled treat_pooled treat_pooledXrunning_pooled) 


**RD FOR NUMBER KILLED

reg numberkilled running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=2, cluster(year) robust
eststo rd1

reg numberkilled running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=3, cluster(year) robust
eststo rd2

reg numberkilled running_pooled treat_pooled treat_pooledXrunning_pooled i.id05 if abs(running_pooled)<=4, cluster(year) robust
eststo rd3

esttab rd1 rd2 rd3 using 22jun_rdkilled.tex, replace se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001) keep(running_pooled treat_pooled treat_pooledXrunning_pooled)


***///DIFFERENCE IN DIFFERENCES///***

//Replicating Table 5

reg eventocc c.idist##c.laid2 i.year i.id05, cluster(id05)
eststo did1

reg eventocc c.idist##c.laid2 i.year i.id05 if milhq_nbr==1, cluster(id05)
eststo did2

reg eventocc c.idist##c.laid2 i.year i.id05 if milhq_nbrbase==0, cluster(id05)
eststo did3


esttab did1 did2 did3 using table5_replication.tex, replace keep(idist laid2 c.idist#c.laid2 _cons) se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)


//Margins Plots (Table 5) 

set more off



reg eventocc c.idist#c.laid2 i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 
margins, at(laid2=(0(1)7.8) idist=(0, 75, 150))
marginsplot, level (90)

reg eventocc c.idist#c.laid2 i.year i.id05 if milhq_nbr==1, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90)
margins, at(laid2=(0(1)7.8) idist=(0, 75, 150))
marginsplot, level (90)

reg eventocc c.idist#c.laid2 i.year i.id05 if milhq_nbrbase==0, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 
margins, at(laid2=(0(1)7.8) idist=(0, 75, 150))
marginsplot, level (90)


//Replicating Table 6

g leconus = log(usaidecon)
g lecon = log(totaleconaid)


reg eventocc c.lecon##c.idist i.year i.id05, cluster(id05)
eststo excl1

reg eventocc c.lchina##c.idist i.year i.id05, cluster(id05)
eststo excl2

reg eventocc c.laid2##c.idist c.lecon#c.idist i.year i.id05, cluster(id05)
eststo excl3 

esttab excl1 excl2 excl3 using Replication_table6.tex, replace se(2) star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep (lecon idist c.lecon#c.idist c.laid2#c.idist c.lchina#c.idist _cons) 


//marginsplot


reg eventocc c.lecon#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) lecon=(3, 5 , 7.5))
marginsplot, level (90) 


reg eventocc c.lchina#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) lchina=(1,  5, 6))
marginsplot, level (90) 


reg eventocc c.laid2#c.idist c.lecon#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 
margins, at(laid2=(0(1)7.8) idist=(0, 75, 150))
marginsplot, level (90)


//Replicating Table 7

reg e_state_on_civ c.laid2##c.idist i.year i.id05, cluster(id05)
eststo kind1

reg e_state_sel c.laid2##c.idist i.year i.id05, cluster(id05)
eststo kind2

reg e_g_state c.laid2##c.idist i.year i.id05, cluster(id05)
eststo kind3

reg e_conv_state c.laid2##c.idist i.year i.id05, cluster(id05)
eststo kind4 

esttab kind1 kind2 kind3 kind4 using 22jun_kind.tex, replace se(2)star(+ 0.1 * 0.05 ** 0.01 *** 0.001) ar2 keep (idist laid2 c.laid2#c.idist) 

//marginsplot

reg e_state_on_civ c.laid2#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 

reg e_state_sel c.laid2#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 


reg e_g_state c.laid2#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 


reg e_conv_state c.laid2#c.idist i.year i.id05, cluster(id05)
margins, at(idist=(0(50)250) laid2=(0,  4, 7.8))
marginsplot, level (90) 

//Karachi Effect (Appendix Table A4) 

reg eventocc c.laid2##c.idist i.year i.id05 if id05!=44, cluster(id05)
eststo rob1

reg eventocc c.laid2##c.idist i.year i.id05 if year<2004, cluster(id05)
eststo rob2

esttab rob1 rob2 using 22jun_rob.tex, replace se(2) star(+ 0.11 * 0.05 ** 0.01 *** 0.001) ar2 keep (idist laid2 c.laid2#c.idist)


//Islamabad Neighbor Effect (Appendix Table A5)

reg eventocc c.idist##c.laid2 i.year i.id05 if isl_neighbor==0, cluster(id05)
eststo isl1

reg eventocc c.idist##c.laid2 i.year i.id05 if isl_neighbor2==0, cluster(id05)
eststo isl2

esttab isl1 isl2 using isl_table.tex, replace keep(idist laid2 c.idist#c.laid2 _cons) se(2) ar2 star(+ 0.1 * 0.05 ** 0.01 *** 0.001)


